<template>
        <a-form-model ref="form" :rules="rules" :model="form"  layout="vertical" class="rx-a-form-vertical" >
            <a-form-model-item :label="i18n('form.pageCustom','页面定制')" prop="dialogVal">
                <rx-input-button v-model="form.dialogVal"
                                 @click="selectCustomView"

                >
                </rx-input-button>
            </a-form-model-item>
            <a-form-model-item :label="i18n('form.paramConfig','参数配置')">
                <a-table :dataSource="localConfig.params" :pagination="false"
                         rowKey="idx_">
                    <a-table-column :title="i18n('form.pageParam','页面定制参数')" dataIndex="bindFieldLabel" width="150px">
                        <template slot-scope="text, record">
                            <a-form-item class="tableItems">
                                {{ record.destLabel }}
                            </a-form-item>
                        </template>
                    </a-table-column>
                    <a-table-column :title="i18n('form.listFields','列表字段')" dataIndex="returnField" width="300px">
                        <template slot-scope="text, record">
                            <a-select
                                v-model="record.srcField" :options="fields" allowClear/>
                        </template>
                    </a-table-column>
                </a-table>
            </a-form-model-item>
        </a-form-model>

</template>

<script>
import {RxDialog, Util,BaseFormModel} from "@lowcode/jpaas-common-lib";
import CustomViewDialog from "@/views/modules/form/core/dialog/CustomViewDialog";
import linkBase from "@/views/modules/form/core/cellRender/linkBase";

/**
 * 数据结构:
 * {
 *     //别名
 *     alias: "",
 *     //名称
 *     name: "",//名称
 *     params:[ {destLabel:"目标参数显示名称",destValue:"目标参数",srcField:"来源字段"}]
 * }
 */

export default {
    name: "link-custom-component",
    mixins:[linkBase,BaseFormModel],
    props: {
        layerid: {type: String},
        lydata: {type: Object},
        destroy: Function,
        config:{type: [String,Object]},
        fields:{type:Array}
    },
    data() {
        return {
            localConfig: {
                alias: "",//alias
                name: "",//名称
                params:[]
            },
            form:{
                dialogVal:""
            },
            rules: {
                dialogVal: [
                    {required: true, message: this.i18n('msa.pageNeed','页面定制必填'), trigger: 'blur'},
                ],
            }

        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formCellRender",key);
        },
        parse(setting){
            if(typeof setting=="string"){
                return JSON.parse(setting);
            }
            return setting;
        },
        init() {
            if(this.config){
                let obj=this.parse(this.config);
                if(obj.hasOwnProperty("alias")){
                    this.localConfig=obj;
                    this.form.dialogVal = JSON.stringify({text: this.config.name, value: this.config.alias});
                }
            }
        },
        getData(){
            return this.localConfig;
        },
        selectCustomView(){
            var self_=this;
            var config = {
                curVm: this,
                widthHeight: ['800px', '600px'],
                component: CustomViewDialog,
                title: self_.i18n('form.pageCustom','页面定制'),
            };
            Util.open(config, function (action,data) {
                if (action != 'ok') return;
                self_.form.dialogVal = JSON.stringify({text: data.name, value: data.alias});
                self_.localConfig.alias=data.alias;
                self_.localConfig.name=data.name;

                self_.localConfig.params=[];
                //"label": "用户主键","value": "pkId"
                if(data.params){
                    var params=JSON.parse(data.params);
                    for (let i = 0; i < params.length; i++) {
                        let obj=params[i];
                        self_.localConfig.params.push({
                            destLabel:obj.label,
                            destValue:obj.value,

                            srcField:""
                        })
                    }
                }
            })
        },
        valid(){
            return new Promise((resolve,reject)=>{
                this.$refs.form.validate(valid => {
                    if(valid){
                        resolve(true)
                    }
                    else{
                        reject(false);
                    }
                });
            })
        },

    },

}
</script>

<style scoped>

</style>
